package com.ce.thd;

import java.util.List;
import java.util.Stack;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ProgressBar;

import com.ce.po.NTree;
import com.ce.po.NTreeItem;
import com.ce.po.VChannelDO;
import com.ce.po.VFilmDO;
import com.ce.po.VSeriesDO;
import com.ce.po.VSetDO;
import com.ce.po.VTypeDO;
import com.ce.utils.DownloadRes;
import com.ce.utils.EtvNetWork;
import com.ce.utils.FileOperate;
import com.ce.utils.FileResDownload;
import com.ce.utils.NWTransform;

public class WritePropertiesThread extends Thread {
	private NTree tree;
	private ProgressBar progressBar;
	private Display display;
	private String path;
	private int iCheck = 0;
	private int iMax = 0;
	Stack<String> stock = new Stack<String>();
	public WritePropertiesThread(NTree tree, ProgressBar progressBar,
			Display display, String path) {
		this.tree = tree;
		this.progressBar = progressBar;
		this.display = display;
		this.path = path;
		iMax = this.progressBar.getMaximum();
	}


	@Override
	public void run() {
		try {
//			if(!this.display.isDisposed()) {
//				Runnable runnable = new Runnable() {
			Thread th = new Thread(new Runnable() {
					
					private FileOperate fileOperate = null;
					private int pages = 1;
					public void run() {
						try {
							fileOperate = new FileOperate(path);
							fileOperate.prepare();
							travelTree(tree);
							fileOperate.flush();
							fileOperate.finish();
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
					
					private void travelTree(Object otree) {
						if(otree instanceof NTree) {
							NTree t = (NTree)otree;
							NTreeItem[] items = t.getItems();
							for (int i = 0; i < items.length; i++) {
								if(items[i].getChecked()){
									String channel = ((VChannelDO)items[i].getData()).getChannel();
									String tagsJson;
									try {
										tagsJson = EtvNetWork.getTags(channel);
										fileOperate.writeTagProperties(tagsJson, channel);
										Thread.sleep(500);
									} catch (Exception e) {
										e.printStackTrace();
									}
									iCheck++;
									//progressBar.setSelection(iCheck);
									//System.out.println(""+progressBar.getMaximum()+"-"+iCheck);
									travelTree(items[i]);
								}
							}
						}
						if(otree instanceof NTreeItem) {
							NTreeItem node = (NTreeItem)otree;
							if(node.getChecked()) {
								if(node.getItemCount() == 0)
									return;
								NTreeItem[] items = node.getItems();
								for (int i = 0; i < items.length; i++) {
									if(items[i].getChecked()){
										if(items[i].getData("type") instanceof VTypeDO) {
											VTypeDO typeDO = (VTypeDO) items[i].getData("type");
											VChannelDO channelDO = (VChannelDO) items[i].getData("channel");
											for (int j = 1; j <= pages; j++) {
												String page = ""+j;
												String type = typeDO.getId();
												String listJson;
												try {
													listJson = EtvNetWork.getList(channelDO.getChannel(), type, page);
													fileOperate.writeListProperties(listJson, channelDO.getChannel(), type, page);
													Thread.sleep(500);
													List<VFilmDO> films = NWTransform.transformJsonToList(listJson);
													for (VFilmDO filmDO : films) {
														stock.add(filmDO.getPic());
														String detailJson = EtvNetWork.getDetail(filmDO.getId(), filmDO.getType());
														fileOperate.writeDetailProperties(detailJson, filmDO.getId(), filmDO.getType());
														Thread.sleep(500);
														//写文件
														VSeriesDO seriesDO = NWTransform.transformJsonToVSeriesDO(detailJson);
														if(seriesDO == null)
															seriesDO = NWTransform.transformJsonToVSeriesDO(detailJson);
														if(seriesDO == null)
															break;
														List<VSetDO> sets = seriesDO.getSets();
														if(sets != null && sets.size()>0) {
															for (VSetDO setDO : sets) {
																if(setDO.getVideoType().equals("2")) {
																	System.out.println(setDO.getSource());
//																	FileResDownload.downloadSwf(setDO.getSource());
//																	stock.add(setDO.getSource());
																	stock.push(setDO.getSource());
																}
															}
														}
													}
												} catch (Exception e) {
													e.printStackTrace();
												}
											}
										}
										iCheck++;
										//progressBar.setSelection(iCheck);
										//System.out.println(""+progressBar.getMaximum()+"-"+iCheck);
										travelTree(items[i]);
									}
								}
							}
						}
					}
			});
			th.start();
				//display.asyncExec(runnable); 
				//display.syncExec(runnable);
		
//			}
			
			Thread tc1 = new Thread(new DownloadRes(stock, "thread-1"));
			tc1.start();
			Thread tc2 = new Thread(new DownloadRes(stock, "thread-2"));
			tc2.start();
			Thread tc3 = new Thread(new DownloadRes(stock, "thread-3"));
			tc3.start();
			while(true) {
				Thread.sleep(1000);
				if(!this.display.isDisposed()) {
					Runnable runnable = new Runnable() {

						public void run() {
							progressBar.setSelection(iCheck);
							System.out.println(""+progressBar.getMaximum()+"-"+iCheck);		
						}
					
					};
					display.asyncExec(runnable); 
				}
				if(iCheck >= iMax)
					break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
